<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close
             @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item :label="$t('procure.order.order')" prop="purchase_id">
                        <div style="display:flex;width:100%">
                            <el-input v-model="purchase_code" :placeholder="$t('procure.order.order')" readonly @click="checkProduct"></el-input>
                            <el-button type="info" icon="el-icon-Close" @click="clearProduct"></el-button>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('procure.house.consignee')" prop="collect_staff">
                        <el-select v-model="form.collect_staff" filterable clearable :placeholder="$t('all.select')" style="width:100%">
                            <el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('sale.sale.date')" prop="open_date">
                        <el-date-picker v-model="form.open_date" type="date" :placeholder="$t('all.select')" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('procure.house.indate')" prop="entry_date">
                        <el-date-picker v-model="form.entry_date" type="date" :placeholder="$t('all.select')" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('goods.goods.ware')" prop="ware_id">
                        <el-select v-model="ware_id" filterable style="width: 100%" :placeholder="$t('all.select')" @change="wareChange" clearable>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item :label="$t('all.remark')" prop="remark">
                    <el-input v-model="form.remark" :placeholder="$t('all.remark')" type="textarea" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-form-item :label="$t('procure.order.orderlist')" prop="detail">
                <!-- <el-button type="primary" size="small" icon="el-icon-plus" @click="checkProduct">选择采购单</el-button> -->
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" :placeholder="$t('all.noData')" height="400" hideAdd>
                    <el-table-column :label="$t('goods.goods.name')" align="center">
                        <template #default="scope">
                            {{scope.row.goods_name}}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('goods.goods.code')" align="center">
                        <template #default="scope">
                            {{scope.row.goods_code}}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('goods.unit.unit')" align="center" prop="unit_name"></el-table-column>
                    <el-table-column :label="$t('procure.order.number')" align="center" prop="produce_number"></el-table-column>
                    <el-table-column :label="$t('procure.house.innumber')" align="center" prop="all_number"></el-table-column>
                    <el-table-column :label="$t('procure.house.remain')" align="center" prop="surplus_number"></el-table-column>
                    <el-table-column :label="$t('all.number')" prop="number" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.number" :placeholder="$t('all.number')" @focus="onFocus($event)" @input="count(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column :label="$t('all.price')" prop="price" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.price" :placeholder="$t('all.price')" @input="totalCompute" @focus="onFocus($event)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('all.subtotal')" prop="total_price" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.total_price" :placeholder="$t('all.subtotal')" readonly></el-input>
                        </template>
                    </el-table-column> -->
                    <el-table-column :label="$t('goods.goods.ware')" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.ware_id" filterable style="width: 100%" :placeholder="$t('goods.goods.ware')">
                                <!-- @change="getLocationList(scope.row)" -->
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="库位" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.location_id" filterable style="width: 100%" placeholder="库位" >
                                <el-option v-for="item in scope.row.locationList" :key="item.id" :label="item.position_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column> -->
                    <el-table-column :label="$t('all.remark')" prop="remark" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" :placeholder="$t('all.remark')"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
            <el-form-item :label="$t('all.attachment')" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">{{$t('all.selectattachment')}}</el-button>
                </sc-upload-file>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >{{$t('all.cancel')}}</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit(0)">{{$t('all.save')}}</el-button>
			<el-button v-if="mode == 'add'" type="primary" :loading="isSaveing" @click="submit(1)">{{$t('all.savecheck')}}</el-button>
		</template>
	</el-dialog>
    <select-order v-if="dialog.order" ref="orderDialog" @success="orderUpdate" @closed="dialog.order=false" :query="{status:1}"></select-order>
</template>

<script>
import selectOrder from '../order/selectOrder.vue';
	export default {
        components: {  selectOrder },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
                    add: this.$t('all.add'),
                    edit: this.$t('all.edit'),
                    show: this.$t('all.show')
				},
				visible: false,
				isSaveing: false,
				form: {
					id:"",
					purchase_id: "",
					collect_staff: "",
					open_date: this.moment().format('YYYY-MM-DD'),
					entry_date: this.moment().format('YYYY-MM-DD'),
					remark: "",
					enclosure: "",
                    detail:[],
                    is_check: 0,
				},
                //验证规则
                rules: {
                    purchase_id: [{ required: true, message: this.$t('all.select') + ' ' + this.$t('procure.order.order')}],
                    collect_staff: [{ required: true, message: this.$t('all.select') + ' ' + this.$t('procure.house.consignee')}],
                },
                addTemplate:{},
                dialog: {
                    order: false,
                },
                adminList:[],
                wareList:[],
                purchase_code: "",
                ware_id: "",
			}
		},
		mounted() {
            this.getUser()
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(e){
                this.form.is_check = e
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res  = [];
                        if (this.form.id) {
                            res = await this.$API.procure.inorder.edit.post(this.form);
                        }else{
                            res = await this.$API.procure.inorder.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res!=''){
                            if(res.code == 1){
                                this.visible = false;
                                this.$emit('success', this.form, this.mode,e)
                                this.$message.success(this.$t('all.yes'))
                            }else{
                                this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
                            }
                        }
                    }
                })
			},
			//表单注入数据
			async setData(data){
				this.form.id = data.id
                var res = await this.$API.procure.inorder.detail.get({id:data.id});
                this.form.purchase_id = res.data.purchase_id
                this.purchase_code = data.purchase&&data.purchase.code
                this.form.collect_staff = res.data.collect_staff
                this.form.purchase_staff = res.data.purchase_staff
                this.form.open_date = res.data.open_date
                this.form.entry_date = res.data.entry_date
                this.form.remark = res.data.remark
                this.form.enclosure = res.data.enclosure
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        id:item.id,
                        goods_id:item.goods_id,
                        goods_name:item.goods && item.goods.goods_name,
                        unit_name:item.goods && item.goods.unit && item.goods.unit.unit_name,
                        goods_code:item.goods && item.goods.goods_code,
                        produce_number:item.start_number,
                        all_number:item.all_number,
                        surplus_number:item.start_number - item.all_number,
                        number:item.number,
                        price:item.price,
                        total_price:item.total_price,
                        remark:item.remark,
                        ware_id:item.ware_id,
                        // locationList:item.locationList,
                        // location_id:item.location_id,
                    })
                })
                this.form.detail = array
			},
            // 选择采购单
            checkProduct(){
                this.dialog.order = true
                this.$nextTick(() => {
                    this.$refs.orderDialog.open('add')
                })
            },
            // 选择采购单返回
            async orderUpdate(data){
                this.form.purchase_id = data.id
                this.purchase_code = data.code
                var res = await this.$API.procure.order.detail.get({id:data.id});
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        goods_id:item.goods_id,
                        goods_name:item.goods && item.goods.goods_name,
                        unit_name:item.goods && item.goods.unit&&item.goods.unit.unit_name,
                        goods_code:item.goods && item.goods.goods_code,
                        produce_number:item.number,
                        all_number:item.all_number,
                        surplus_number:item.number - item.all_number,
                        number:item.number - item.all_number,
                        price:item.price,
                        total_price:item.total_price,
                        remark:item.remark,
                        ware_id:this.ware_id?this.ware_id:'',

                    })
                })
                this.form.detail = array
            },
            clearProduct(){
                this.form.purchase_id = ""
                this.purchase_code = ""
                this.form.detail = []
            },
            // 获取收货人
            async getUser(){
                var res = await this.$API.system.admin.all.get();
                this.adminList = res.data;

            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.goods.ware.all.get();
                this.wareList = res.data;
                if(this.mode == 'add'){
                    res.data.map(item=>{
                        if(item.is_default == 1){
                            this.ware_id = item.id
                        }
                    })
                }
            },
            async getLocationList(row){
                const res = await this.$API.goods.ware.position.post({ware_id:row.ware_id})
                row.locationList = res.data
            },
            onFocus(event){
                event.target.select();
            },
            wareChange(e){
                this.form.detail.map(item => item.ware_id = e)
            },
            count(row){
                if(row.number > row.surplus_number){
                    this.$message.error(this.$t('procure.house.prompt'))
                    row.number = row.surplus_number
                }
            },
		}
	}
</script>

<style>
</style>
